package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.device.csun.sleep.entity.CsunSleepData;
import com.csun.cmny.provider.device.sleepcare.entity.pojo.SleepCareUserInfo;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcSleepCareData;
import com.csun.cmny.provider.model.dto.sleep.BedOldmanSleepReqDto;
import com.csun.cmny.provider.model.vo.DmcSleepCareVo;
import com.csun.cmny.provider.model.vo.SleepCareDataReqVo;
import com.csun.cmny.provider.model.vo.csunsleep.CsunSleepRealDataVo;
import com.csun.cmny.provider.model.vo.csunsleep.DmcCsunSleepVo;
import com.csun.cmny.provider.model.vo.sleepcare.SleepCareRealDataVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcSleepCareBindService;
import com.csun.cmny.provider.service.DmcSleepCareDataService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-02-23 14:34
 * @Description: the class dmc sleep care data service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcSleepCareDataServiceImpl extends BaseService<DmcSleepCareData>
        implements DmcSleepCareDataService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcSleepCareBindService dmcSleepCareBindService;

    /**
     * query sleep care real data by device id
     *
     * @param deviceId device id
     * @return sleep care real data vo
     */
    @Override
    public SleepCareRealDataVo querySleepCareRealDataByDeviceId(Long deviceId) {

        // 1.校验智能床垫 - 睡眠卫士
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsSleepCareByDeviceId(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610037, deviceId);
        }

        // 2.判断智能床垫 - 睡眠卫士是否已经绑定
        DmcSleepCareVo dmcSleepCareVo = dmcSleepCareBindService.getSleepCareBindByDeviceId(deviceId);
        if (PublicUtil.isEmpty(dmcSleepCareVo)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610039, deviceId);
        } else {
            if (PublicUtil.isEmpty(dmcSleepCareVo.getBindType())) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610039, deviceId);
            }
        }

        // 4.从redis中获取数据
        String sleepCareDataKey = RedisKeyUtil.getSleepCareKey(dmcSleepCareVo.getDeviceNo());
        SleepCareRealDataVo sleepCareRealDataVo = new SleepCareRealDataVo();
        SleepCareUserInfo userInfo = (SleepCareUserInfo) redisTemplate.opsForValue().get(sleepCareDataKey);
        if (PublicUtil.isEmpty(userInfo)) {
            userInfo = new SleepCareUserInfo();
        }

        BeanUtils.copyProperties(dmcSleepCareVo, sleepCareRealDataVo);
        BeanUtils.copyProperties(userInfo, sleepCareRealDataVo);
        sleepCareRealDataVo.setDeviceId(dmcSleepCareVo.getId());
        sleepCareRealDataVo.setBind(true);

        return sleepCareRealDataVo;
    }

    /**
     * query sleep care data by bed oldman list
     *
     * @param bedOldmanSleepReqDtoList bed oldman sleep req dto list
     * @return the list.
     */
    @Override
    public List<SleepCareDataReqVo> querySleepCareDataByBedOldmanList(List<BedOldmanSleepReqDto> bedOldmanSleepReqDtoList) {

        List<SleepCareDataReqVo> sleepCareDataReqVoList = dmcSleepCareBindService.querySleepCareDataByBedOldmanList(bedOldmanSleepReqDtoList);
        for (SleepCareDataReqVo sleepCareDataReqVo : sleepCareDataReqVoList) {
            SleepCareUserInfo userInfo = (SleepCareUserInfo) redisTemplate.opsForValue().get(RedisKeyUtil.getSleepCareKey(sleepCareDataReqVo.getDeviceNo()));
            if (PublicUtil.isNotEmpty(userInfo)) {
                BeanUtils.copyProperties(userInfo, sleepCareDataReqVo);
            }
        }

        return sleepCareDataReqVoList;
    }
}
